home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / Rewrite 0.2.6 / ReWrite 0.2.6 Docs / ReWrite 0.2.6 Docs.rsrc / TEXT_131.txt < prev    next >
Encoding:
Text File  |  1995-08-23  |  7.8 KB  |  223 lines

  1.  
  2. Chapter 3 - Language Definition
  3.  
  4. This section (semi-)formally describes the language. See Appendix 1 (Conventions) to see a description of what type face is used where.
  5.  
  6. Definitions
  7.  
  8. ReWrite supports only lists and 32-bit integers internally. However, for purposes of documentation the following types will be used: 
  9.  
  10. integer  (int )      32-bit integer
  11.  
  12. boolean  (bool )  either true or false
  13.  
  14. char  (char )        a character, represented as "char "
  15.  
  16. list  (list )          a list of zero or more values, denoted {val , .. ,val }
  17.                   Note: this is represented as lis  in ReWrite itself.
  18.  
  19. token-string (sym )
  20.                               a token-string, represented `token-string
  21.  
  22. null (null )      this is a data-less type, similar to void in C.
  23.                 Note: this has no type representation in ReWrite - it is just a constant.
  24.  
  25. value (val )               a list or other atomic type. Basically anything.
  26.                              This is just used for documentation purposes - it is not an actual type.
  27.  
  28.    In addition other structures will be used (set etc). they will be defined as needed.
  29.  
  30.     A trailing -s  is used to indicate a sequence of zero or more of these. For example,
  31. {ints }  is the same as  {int , .. ,int }, and represents a list containing one or more integers.
  32.  
  33. (directly coded) -    a hand coded function. These have been written directly in the intermediate language so as to run fast.
  34.  
  35. ReWrite Syntax
  36.  
  37. For the following, a token is considered to be any sequence of letters and numbers, starting with a letter. (an underscore is a letter). Note that ReWrite is case sensitive.
  38.  
  39. rules
  40.  
  41. A rewrite rule is something of one of the following forms:
  42.  
  43. name [patterns ] -> results ;
  44. name [patterns ]::condition -> results ;
  45.  
  46. where
  47.    name        is a token,
  48.    patterns    is zero or more patterns , separated by commas,
  49.    condition   is an expression ,
  50.    results     is zero or more expressions , separated by commas.
  51.  
  52. patterns
  53.  
  54. A pattern  is one of the following:
  55. ‚Ä¢ n   where n is a constant . This will match with the constant n . (see below)
  56. ‚Ä¢ x   where x  is a token.
  57.         This will match with any one value.
  58.         x  will be given this value for the duration of the rule.
  59. ‚Ä¢ x :type     where type  is one of int , lis , bool , char or sym  (see below).
  60.         This will match with any one value of type type .
  61.         x  will be given this value for the duration of the rule.
  62. ‚Ä¢ n :type     a constant coerced to that type.
  63. ‚Ä¢ {pp }    where pp  is zero or more patterns , separated by commas.
  64.         The will match with a list, where the patterns pp must match the inside of the list.
  65. ‚Ä¢  .x     where x  is a token.
  66.         This will match with zero or more values.
  67.         x  will be given a list containing these values for the duration of the rule.
  68. ‚Ä¢ Any of the tokensx given above may be replaced by an _ (underscore).
  69.   This matches with anything that a token would.
  70.   Multiple _  in a pattern may refer to different objects.
  71.  
  72. types
  73.  
  74. In the type  field above,
  75. int  specifies an integer,
  76. lis  specifies a list,
  77. bool specifies a boolean (either true or false),
  78. char specifies a character,
  79. sym  specifies a symbol (henceforth called a token-string).
  80.  
  81.    A token-string will be a constant fixed by the system for each 'string'. For example, if I was to use the symbol `x, I could be sure that is was equal to itself, and different to anything else, and it only uses up as much storage as an integer for each use after the first. This would allow manipulation of expressions.
  82.  
  83. expressions
  84.  
  85. An expression  is any of the following:
  86. ‚Ä¢ n        where n  is a constant .
  87. ‚Ä¢ x        where x  is a token.
  88. f [exprs ]    where f  is a token (a function name), and 
  89.         exprs  is a list or zero or more expressions  separated by commas.
  90. ‚Ä¢ expression :type  is an expression coerced to the appropriate type.
  91.  
  92. coercions allowed:
  93. integer <-> character
  94. integer <-> token-string  (this is provided for the compiler, and should not be used).
  95.  
  96. Note: f [exprs ] includes all the infix functions, and short forms, since these are just shorthand for f [exprs ] so:
  97.   .x
  98.   1+g[2]
  99.   {0,.b}
  100. are all expressions .
  101.  
  102. constants
  103.  
  104. A constant may be:
  105. ‚Ä¢ an integer constant,
  106. ‚Ä¢ a boolean:
  107.    - either true or false,
  108. ‚Ä¢ a null:
  109.    - null,
  110. ‚Ä¢ a character:
  111.    - represented "char ", for example "a",
  112. ‚Ä¢ a token-string:
  113.    - represented `token-string , for example `thistoken,
  114.  
  115. An integer constant  is one of the following:
  116. ‚Ä¢     a sequence of digits,
  117. ‚Ä¢ $    followed by a sequence of hexadecimal digits,
  118. ‚Ä¢ %    followed by a series of binary digits,
  119. ‚Ä¢ -    followed by any of the above,
  120. ‚Ä¢ maxint,
  121. ‚Ä¢ minint.
  122.  
  123. Other shorthand forms and syntax:
  124. "string "     allows several characters to be typed together.
  125.  
  126. For example:
  127.         "abc"
  128.     is equivalent to
  129.         "a","b","c"
  130.  
  131. 'string '    is shorthand for typing in the ASCII values for the string entries. (This is shortly to be removed, and only remains because the compiler uses it).
  132.  
  133. For example:
  134.         'abc'
  135.     is equivalent to
  136.         97,98,99
  137.  
  138. (* comment  *)    is how comments are done.
  139.  
  140. Note that comments are fully recursive.
  141.  
  142. ReWrite Semantics
  143.  
  144. Recall that rewrite calls are of the form:
  145.  
  146.   name [patterns ] -> results ;
  147.   name [patterns ]::condition -> results ;
  148.  
  149. This is how rules can be thought of as being evaluated:
  150. ‚Ä¢ If an expression name [values ] is encountered during execution of code, a rule starting with the same name  is searched for in the order last file in project to first file in project then top of file to bottom of file. For example, if the project consisted of two files, setup and go in that order, the search will (after first checking the internal libraries) start at the top of go and finish at the bottom of setup.
  151. ‚Ä¢ As each rule is found, the values are matched with the patterns and if that succeeds then the condition (if there is one) is checked, and on success if that, the right hand side is evaluated. If either if these checks fail, then the search for the correct rule continues. If no matching rule is found, then an error is generated, giving the name of the rule that failed and the arguments that were passed to it.
  152.  
  153. Note: when Go is selected in the Project menu, then execution always starts with the expression
  154.   top[]
  155.  
  156. Precedence of operations
  157.  
  158. (similar to C apart from the not (!) operator)
  159.  
  160. ()  []  {} (list)
  161. : (type coercion)
  162. . (splice)
  163. *  /  %
  164. +  -
  165. <  <=  >  >=
  166. =  !=
  167. !
  168. &
  169. | (or)
  170.  
  171. Some features
  172.  
  173. The above syntax results in some features that are worth pointing out:
  174.  
  175. ‚Ä¢ Functions can have a variable number of arguments.
  176.    Because the arguments passed to a function can be considered a list, the splice notation can be used for picking up parts of it.
  177.  
  178. For example,
  179.   second[a,b,.x] -> b;
  180.  
  181. returns the second argument passed,
  182.  
  183.   last[.x,a] -> a;
  184.  
  185. returns the last argument passed.
  186.  
  187. ‚Ä¢ Functions can return an arbitrary number of results, including zero.
  188.  
  189. For example,
  190.   g[0] -> ;
  191.   g[n:int] -> g[n-1],n;
  192.  
  193. will return the results 1,2,3, ... ,n  not in a list.
  194.  
  195. If these results were wanted in a list then just use the list brackets, so
  196.   {g[10]} ->  {1,2,3,4,5,6,7,8,9,10}
  197.  
  198.    Also, the case of returning nothing is not the same as returning null - the null is still a type (albeit one that contains no information), whereas returning nothing is just returning zero results.
  199.  
  200. ‚Ä¢ Functions can 'pick up' a variable number of arguments.
  201.  
  202. For example, (with g defined as above)
  203.   add[1000,2000] -> 3000;
  204.   add[1000,g[0],2000] 
  205.     -> add[1000,2000]
  206.     -> 3000;
  207.   add[1000,g[10],2000]
  208.     -> add[1000,1,2,3,4,5,6,7,8,9,10,2000]
  209.     -> 3055;
  210.  
  211. If h was defined as
  212.   h[x] -> {g[x]};
  213.  
  214. the same result could be achieved by
  215.   add[1000,.h[10],2000]
  216.     -> add[1000,.{1,2,3,4,5,6,7,8,9,10},2000]
  217.     -> add[1000,1,2,3,4,5,6,7,8,9,10,2000]
  218.     -> 3055;
  219.  
  220.    These features make it unnecessary to actually use lists unless there is information that needs to be grouped (although lists can make code clearer), and gives extra flexibility to the language.
  221.    Note that list ({}) and splice (.) are very powerful for getting the right level of list brackets.
  222.  
  223.